/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution
{
public:
    bool isBalanced(TreeNode *root)
    {
        return height(root) != -1;
    }

    int height(TreeNode *tree)
    {
        if (tree == nullptr)
        {
            return 0;
        }
        int leftHeight = height(tree->left);
        if (leftHeight == -1)
        {
            return -1;
        }
        int rightHeight = height(tree->right);
        if (rightHeight == -1)
        {
            return -1;
        }
        if (rightHeight - leftHeight > 1 || rightHeight - leftHeight < -1)
        {
            return -1;
        }
        return max(leftHeight, rightHeight) + 1;
    }
};